$(function(){
    var username = $.cookie('username')
    var header = $.cookie('header')
    if(!username){
        location.href = 'login.html'
    }

    //点击提问按钮跳转到提问页面
    $('.ask').click(function(){
        location.href = 'ask.html'
    })
    if(header != 'undefined'){
        $('.header img').attr('src','uploads/'+ header)
        $('.header-img img').attr('src','uploads/'+ header)
    }else{
        $('.header img').attr('src','img/user.png')
        $('.header-img img').attr('src','img/user.png')
    }

    if(username){
        $('.username').find('.header span').text(username)
            .end().show().siblings('.login').hide()

        //发起请求去获取用户的信息
        var data = 'username=' + username
        $.get('/user',data)
            .done(function(data){
                //$('.mui-table-view li').eq(1).text(data.data.username)
                $('.am-list li').each(function(i,el){
                     
                    if($(el).data('info')){
                       $(el).find('span').text(data.data[$(el).data('info')])
                       if(i == 2){
                           var sex = data.data[$(el).data('info')] == 'true' ? '男' : '女'
                           $(el).find('span').text(sex)
                       }
                       if(i == 5){
                           $(el).find('span').text(changeTime(data.data[$(el).data('info')]))
                        //   console.log(data.data[$(el).data('info')])
                       }
                    }
                })
            })

            .fail(function(err){
                console.log(err)
            })
    }else{
        $('.username').hide().siblings('.login').show()
    }

    $('.header').click(function(){
        $(this).next().toggle()
    })


    $('.username ul li').first().click(function(){
        location.href = 'user.html'
    })
    $('.username ul li').last().click(function(){
        $.removeCookie('username')
        location.reload()
    })
      $('.header-img img').click(function(){
        $('.am-btn').click()
    })
    //点击上传头像进行上传
    $('.am-btn').click(function(){
        $('.header-img input[name=header]').click()
    })
   
       
     $('form').submit(function(e){

        e.preventDefault();
        var data=new FormData();


        $.post({
            url:'/header',
            data:data,
            contentType:false,
            processData:false
        }).done(function(data){
            if(data.code == 'success'){
                console.log(data)
                // location.reload()
            }
        }).fail(function(err){
            console.log(err)
        })
    })

    function changeTime(time){
        time = new Date(time)
        var year = time.getFullYear()
        var month = time.getMonth() + 1
        var day = time.getDate()

        var hour = time.getHours()
        var minute = time.getMinutes()
        var second = time.getSeconds()
        month = month > 9 ? month : '0' + month
        day = day > 9 ? day : '0' + day
        hour = hour > 9 ? hour : '0' + hour
        minute = minute > 9 ? minute : '0' + minute
        second = second > 9 ? second : '0' + second
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    }


})